package com.zhuge_new.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.zhuge_new.Adapter.DirectAdapter;
import com.zhuge_new.Base.BaseFragment;
import com.zhuge_new.R;
import com.zhuge_new.bean.DirectBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.PreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by lenovo on 2018-09-15.
 */

public class DirectFragment extends BaseFragment {

    private SwipeMenuRecyclerView recyclerView;
    private DirectBean directBean;
    private String grade;
    private ArrayList list=new ArrayList();
    protected boolean isViewInitiated;//判断视图是否初始化
    protected boolean isVisibleToUser;//用户是否可见
    protected boolean isDataInitiated;//数据是否初始化
    private int num=1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.direct_fragment, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        grade = getArguments().getString("grade");
        init_title();
    }

    @Override
    public void init_title() {
        super.init_title();
        recyclerView = getActivity().findViewById(R.id.recycle_list);
    }

    @Override
    public void getData() {
        super.getData();
        Log.e(grade,"打印接收数据");
        switch (grade) {
            case "2":
                OkGo.<String>post(BaseHttp.user_children_datas)
                        .headers("token", PreferencesUtils.getString(getContext(), "token"))
                        .params("grade", grade)
                        .execute(new StringDialogCallback(getActivity()) {
                            @Override
                            public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                                directBean = (DirectBean) new DataParse().getBean(response.body(), DirectBean.class, 0);
                                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

                            }
                        });
                break;
            case "3":
                OkGo.<String>post(BaseHttp.user_children_datas)
                        .headers("token", PreferencesUtils.getString(getContext(), "token"))
                        .params("grade", grade)
                        .params("rows","15")
                        .params("page","1")
                        .execute(new StringDialogCallback(getActivity()) {
                            @Override
                            public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                                directBean = (DirectBean) new DataParse().getBean(response.body(), DirectBean.class, 0);
                                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

                            }
                        });
                break;
            case "4":
                OkGo.<String>post(BaseHttp.wxshare_bindlist)
                        .params("openid","oaASb1YDZGoa630lgzBW_gBIINsA?from=singlemessage")
                        .params("rows","15")
                        .params("page",num)
                        .execute(new StringDialogCallback(getActivity()) {
                            @Override
                            public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                                try {
                                    JSONObject obj=new JSONObject(response.body());
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

        }

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareFetchData();
    }

    public boolean prepareFetchData(){
        return prepareFetchData(false);
    }
    public boolean prepareFetchData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            getData();
            isDataInitiated = true;
            return true;
        }
        return false;
    }

}
